package com.ql.gamelogm.controller;

import com.alibaba.fastjson.JSONArray;
import com.ql.gamelogm.entities.*;
import com.ql.gamelogm.repository.DataLogRepository;
import com.ql.gamelogm.service.DataLogService;
import com.ql.gamelogm.service.LogService;
import com.ql.gamelogm.utils.IpUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Controller
public class DataController {

    @Autowired
    private DataLogRepository dataLogRepository;

    @Autowired
    private LogService logService;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private DataLogService dataLogService;
    @Autowired
    private JdbcTemplate jdbcTemplate;


    @PostMapping(value = "/findDataLogByCondition")
    public String findDataLogBycondition(Model model, HttpServletRequest httpServletRequest, HttpSession httpSession) throws ParseException {
        Long start = System.currentTimeMillis();
        redisCache.getDataLogRedis();
        //取条件数据、起始时间、结束时间
        String getCondition = httpServletRequest.getParameter("jstj");
        String select=httpServletRequest.getParameter("slct");
        String fdate = httpServletRequest.getParameter("fdate");
        String ldate = httpServletRequest.getParameter("ldate");
        String id=(String)httpSession.getAttribute("id");
        if (!(fdate == null) && !(ldate == null)&&!(getCondition==null)) {
            //将起止时间存入session
            httpSession.setAttribute("fdate", fdate);
            httpSession.setAttribute("ldate", ldate);
            //取出的起止时间日期格式化到ES查询
            Long ftime=0L;
            Long ltime=0L;
            try {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                if(fdate!=""&&ldate!=""){
                    ftime = simpleDateFormat.parse(fdate).getTime();
                    ltime = simpleDateFormat.parse(ldate).getTime();
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
            Date date = new Date();
            date.setTime(start);
            List<DataLog> dataLogList = new ArrayList<DataLog>();
            if(select.equals("人物名称")){
                dataLogList=dataLogRepository.findByGamernameAndDateBetween(getCondition, ftime, ltime);
            }
            if(select.equals("物品名称")){
                dataLogList=dataLogRepository.findByItemnameAndDateBetween(getCondition, ftime, ltime);
            }
            int count = dataLogList.size();
            //返回结果到前端页面展示
            httpSession.setAttribute("count", count);
            Long end = System.currentTimeMillis();
            Long htime = end - start;
            httpSession.setAttribute("htime", htime);
            model.addAttribute("dataList", dataLogList);
            //存搜索记录
            SearchHistory searchHistory = new SearchHistory();
            searchHistory.setOperator(id);
            searchHistory.setDepository("elasticsearch");
            searchHistory.setSearchtime(date);
            searchHistory.setSentence(getCondition);
            searchHistory.setResult("查找出日志数据" + count + "条");
            searchHistory.setTime(String.valueOf(htime) + "ms");
            logService.SaveSearchHistory(searchHistory);
            //存查询日志操作记录

            String ipaddr=IpUnit.getIpAddr();
            Log log=new Log();
            log.setDate(date);
            log.setOperator(id);
            log.setOperation("日志查询");
            log.setIpaddr(ipaddr);
            logService.savelog(log);
        }
        return "LogCatch";
    }
    @GetMapping(value = "/findProblemDataLogs")
    public String searchProblemDataLogs(HttpSession httpSession, Model model, @RequestParam(value = "pageNum",defaultValue = "0")int PageNum, @RequestParam(value = "pageSize",defaultValue = "8")int
            PageSize) throws ParseException {
        String operator=(String)httpSession.getAttribute("id");
        Page<ProblemData> problemData=dataLogService.ProblemLogsList(PageNum,PageSize);
        model.addAttribute("problemData", problemData);
        List<ProblemData> problemDataList=dataLogService.findProblemLogs();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int size=problemDataList.size();
        httpSession.setAttribute("size",size);
        //查操作记录
        Date date=new Date();
        date.setTime(System.currentTimeMillis());
        String datestr=simpleDateFormat.format(date);
        System.out.println(datestr);
        String ipaddr= IpUnit.getIpAddr();
        Log log=new Log();
        log.setDate(date);
        log.setOperator(operator);
        log.setOperation("日志分析");
        log.setIpaddr(ipaddr);
        logService.savelog(log);
        /*String sql="update analysis set recent='"+simpleDateFormat.parse(datestr)+"'";
        jdbcTemplate.update(sql);*/
        return "LogAnalysis";
    }
}
